package com.ymttest.business.service.shenqi;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.shop2cn.iapi.sqmarketing.parameter.SaveOrUpdaateMarketingActivityReq;
import com.shop2cn.iapi.sqmarketing.pojo.ActivityConditionItemPojo;
import com.shop2cn.iapi.sqmarketing.service.SaveOrUpdaateMarketingActivityCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.sqlwapper.sqmarketing.MarketingActivityWrapper;

public class SqmarketingService {

	// 新增满减活动
	public static Long saveOrUpdaateMarketingActivity(Integer conditionType,
			String payCurrency, BigDecimal payAmountThreshold, Long sellerId,
			int countLimit,
			List<ActivityConditionItemPojo> activityConditionItemList)
			throws ClientProtocolException, IOException {

		// 创建优惠券批次
		String batchCode = CouponShop2cnMCallService.createSellerBatch(sellerId);
		
		SaveOrUpdaateMarketingActivityReq saveOrUpdaateMarketingActivityReq = new SaveOrUpdaateMarketingActivityReq();
		SaveOrUpdaateMarketingActivityCall saveOrUpdaateMarketingActivityCall = new SaveOrUpdaateMarketingActivityCall();

		ActivityConditionItemPojo activityConditionItem = new ActivityConditionItemPojo();

		activityConditionItem.setItemList(Arrays.asList("wjehrkwhe"));
		activityConditionItem.setConditionType(1);

		// saveOrUpdaateMarketingActivityReq.setId(id);
		saveOrUpdaateMarketingActivityReq.setSellerId(sellerId);
		saveOrUpdaateMarketingActivityReq.setPayCurrency(payCurrency);
		saveOrUpdaateMarketingActivityReq.setActivityName("activityName");
		saveOrUpdaateMarketingActivityReq.setActivityStatus(1);
		saveOrUpdaateMarketingActivityReq.setActivityConditionItemList(activityConditionItemList);
		saveOrUpdaateMarketingActivityReq.setStartTime("2021-08-03 17:21:05");
		saveOrUpdaateMarketingActivityReq.setEndTime("2022-08-03 17:21:05");
		saveOrUpdaateMarketingActivityReq.setCouponBatchIds(Arrays
				.asList(batchCode));
		saveOrUpdaateMarketingActivityReq.setConditionType(conditionType);
		saveOrUpdaateMarketingActivityReq.setPayAmountThreshold(payAmountThreshold);
		saveOrUpdaateMarketingActivityReq.setCountLimit(countLimit);

		saveOrUpdaateMarketingActivityCall
				.setData(saveOrUpdaateMarketingActivityReq);
		saveOrUpdaateMarketingActivityCall.callService();
		
		return saveOrUpdaateMarketingActivityCall.getId();
	}
	
	// 新增满减活动
	public static void saveOrUpdaateMarketingActivityWithBatchs(Integer conditionType,
			String payCurrency, BigDecimal payAmountThreshold, Long sellerId,
			int countLimit,
			List<ActivityConditionItemPojo> activityConditionItemList)
			throws ClientProtocolException, IOException {

		// 创建优惠券批次
		String batchCode1 = CouponShop2cnMCallService
				.createSellerBatch(sellerId);
		
		String batchCode2 = CouponShop2cnMCallService
				.createSellerBatch(sellerId);

		SaveOrUpdaateMarketingActivityReq saveOrUpdaateMarketingActivityReq = new SaveOrUpdaateMarketingActivityReq();
		SaveOrUpdaateMarketingActivityCall saveOrUpdaateMarketingActivityCall = new SaveOrUpdaateMarketingActivityCall();

		ActivityConditionItemPojo activityConditionItem = new ActivityConditionItemPojo();

		activityConditionItem.setItemList(Arrays.asList("wjehrkwhe"));
		activityConditionItem.setConditionType(1);

		// saveOrUpdaateMarketingActivityReq.setId(id);
		saveOrUpdaateMarketingActivityReq.setSellerId(sellerId);
		saveOrUpdaateMarketingActivityReq.setPayCurrency(payCurrency);
		saveOrUpdaateMarketingActivityReq.setActivityName("activityName");
		saveOrUpdaateMarketingActivityReq.setActivityStatus(1);
		saveOrUpdaateMarketingActivityReq
				.setActivityConditionItemList(activityConditionItemList);
		saveOrUpdaateMarketingActivityReq.setStartTime("2021-08-03 17:21:05");
		saveOrUpdaateMarketingActivityReq.setEndTime("2022-08-03 17:21:05");
		saveOrUpdaateMarketingActivityReq.setCouponBatchIds(Arrays
				.asList(batchCode1, batchCode2));
		saveOrUpdaateMarketingActivityReq.setConditionType(conditionType);
		saveOrUpdaateMarketingActivityReq
				.setPayAmountThreshold(payAmountThreshold);
		saveOrUpdaateMarketingActivityReq.setCountLimit(countLimit);

		saveOrUpdaateMarketingActivityCall
				.setData(saveOrUpdaateMarketingActivityReq);
		saveOrUpdaateMarketingActivityCall.callService();
	}
	
	public static void sqmarketingCallBack(long userId, long mainOrderId) {

		YmatouEasyCall sqmarketingCallBackCall = new YmatouEasyCall("", "POST",
				"JSON");
		StringBuilder sqmarketingCallBackString = new StringBuilder();

		sqmarketingCallBackString.append("{\"userId\": " + userId + ", ");
		sqmarketingCallBackString.append("\"mainOrderId\": \"" + mainOrderId
				+ "\"}");

		sqmarketingCallBackCall
				.setUrl("http://sqmarketing.iapi.shop2cn.com/api/sqmarketingCallBack");

		sqmarketingCallBackCall.setData(sqmarketingCallBackString.toString());

		try {
			sqmarketingCallBackCall.callService();

			Logger.comment(sqmarketingCallBackCall.getReturnData());

		} catch (Exception e) {

			e.printStackTrace();
		}
	}
	
	/**
	 * 将指定sellerId的营销活动删除
	 * @param sellerId
	 */
	public static void deleteAllMarketingActivity(long sellerId) {

		MarketingActivityWrapper marketingActivityWrapper = new MarketingActivityWrapper();

		marketingActivityWrapper.deleteAllMarketingActivity(sellerId);
	}
	
	/**
	 * 清除营销活动缓存
	 */
	public static void deleteCache() {

		YmatouEasyCall sqmarketingCallBackCall = new YmatouEasyCall("", "POST",
				"JSON");
		StringBuilder sqmarketingCallBackString = new StringBuilder();

		sqmarketingCallBackString.append("{}");

		sqmarketingCallBackCall
				.setUrl("http://sqmarketing.iapi.shop2cn.com/api/deleteCache");

		sqmarketingCallBackCall.setData(sqmarketingCallBackString.toString());

		try {
			sqmarketingCallBackCall.callService();

			Logger.comment(sqmarketingCallBackCall.getReturnData());

		} catch (Exception e) {

			e.printStackTrace();
		}
	}
}
